package com.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.AdminMapper;
import com.pojo.Admin;
import com.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Override
    public Admin selectAdminByAccountAndPassword(String account, String password) {
        return adminMapper.selectAdminByAccountAndPassword(account, password);
    }

    @Override
    public PageInfo selectAdminByAccount(Map map) {
        int pageNum = (int)map.get("pageNum");
        PageHelper.startPage(pageNum,2);
        String account = (String)map.get("account");
        List<Admin> admins = adminMapper.selectAdminByAccount(account);
        PageInfo pageInfo=new PageInfo(admins);
        return pageInfo;
    }

    @Override
    public Admin selectAdminById(long id) {
        return adminMapper.selectAdminById(id);
    }

    @Override
    public int updateAdmin(Admin admin) {
        return adminMapper.updateAdmin(admin);
    }

    @Override
    public int insertAdmin(Admin admin) {
        return adminMapper.insertAdmin(admin);
    }

    @Override
    public int deleteAdminById(long id) {
        return adminMapper.deleteAdminById(id);
    }

    @Override
    public int resumeAdminById(long id) {
        return adminMapper.resumeAdminById(id);
    }

    @Override
    public int insertAdminRoles(long id, long[] roles) {
        //1.删除管理员已有的角色
        adminMapper.deleteRoelById(id);
        //2.添加角色
        for (int i=0;i<roles.length;i++){
            long roleid=roles[i];
            adminMapper.addAdminRole(id,roleid);
        }
        return 1;
    }
}
